package com.pp.tourism.web.controller;


import com.pp.tourism.web.dto.ApiResponse;
import com.pp.tourism.web.pojo.ScenicRecommend;
import com.pp.tourism.web.pojo.User;
import com.pp.tourism.web.service.ScenicRecommendService;
import com.pp.tourism.web.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 * 用户景点推荐关联关系表 前端控制器
 * </p>
 *
 * @author wjm
 * @since 2019-03-20
 */
@RestController
@RequestMapping("/scenicRecommend")
public class ScenicRecommendController {
    @Autowired
    private ScenicRecommendService scenicRecommendService;
    @Autowired
    private UserService userService;

    @ApiOperation("景点点赞和取消点赞")
    @PostMapping("/recommend")
    public ApiResponse recommend(@RequestBody Map<String,Object> param){
        try {
            String username = param.get("username").toString();
            //获取用户id  commentImgUrl
            User user = userService.queryUserByName(username);

            ScenicRecommend scenicRecommend = new ScenicRecommend();
            scenicRecommend.setScenicId(Integer.valueOf(param.get("scenicId").toString()));
            scenicRecommend.setUserId(user.getId());

            boolean insert = scenicRecommendService.insert(scenicRecommend);
            return ApiResponse.ok(insert);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return ApiResponse.error("500", e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return ApiResponse.error("500", "操作失败！");
        }
    }
}

